package collector.core.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.Serializable;
import java.util.List;

import net.apexcns.core.util.ConfigUtil;
import net.apexcns.core.util.XmlBindUtil;
import net.apexcns.core.util.xstream.PropsConverter;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

@XStreamAlias("config")
public final class Config implements Serializable {
	/** <pre>
     * Definition of serialVersionUID : type of long
     * <br>
     * </pre>
     */
    private static final long serialVersionUID = 5231785118871124366L;
	private String metaServer;
	private int metaPort;
	private String monCluster;
	private String dbUrl;
	private String dbDriver;
	private String dbUsername;
	private String dbPassword;
	@XStreamConverter(PropsConverter.class)
	private String rrdPath;
	private String appHome;
	private int monPort;
	private String username;
	private String password;

	private long cleanup = 300;	// sec

	@XStreamImplicit(itemFieldName="monTask")
	private List<ConfigMonTask> monTask;

	@XStreamImplicit(itemFieldName="metaExec")
	private List<ConfigMetaExec> metaExec;

	private static final class SingletonHolder {
		static final Config singleton = getConfig();
		private static Config getConfig() {
			try {
				return (Config) XmlBindUtil.fromXml(Config.class, new FileInputStream(new File(ConfigUtil.getCollectorConfigFile())));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			return null;
		}
	}

	private Config() {
	}

	public static Config newInstance() {
		return SingletonHolder.singleton;
	}

	public List<ConfigMonTask> getMonExecGroup() {
		return monTask;
	}

	public String getMetaServer() {
		return metaServer;
	}
	public int getMetaPort() {
		return metaPort;
	}
	public String getRrdPath() {
		return rrdPath;
	}

	public List<ConfigMetaExec> getMetaExec() {
		return metaExec;
	}

	public List<ConfigMonTask> getMonTask() {
		return monTask;
	}

	public String getMonCluster() {
		return monCluster;
	}

	public String getDbUrl() {
		return dbUrl;
	}

	public int getMonPort() {
		return monPort;
	}

	public String getDbDriver() {
		return dbDriver;
	}

	public String getDbUsername() {
		return dbUsername;
	}

	public String getDbPassword() {
		return dbPassword;
	}

	public String getUsername() {
		return username;
	}

	public String getPassword() {
		return password;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString() 
	 * 
	 * Method : collector.core.config.Config.toString()
	 */
	@Override
	public String toString() {
	    StringBuilder builder = new StringBuilder();
	    builder.append("Config [metaServer=");
	    builder.append(metaServer);
	    builder.append(", metaPort=");
	    builder.append(metaPort);
	    builder.append(", username=");
	    builder.append(username);
	    builder.append(", password=");
	    builder.append(password);
	    builder.append(", cleanup=");
	    builder.append(cleanup);
	    builder.append(", apphome=");
	    builder.append(appHome);
	    builder.append(", metaExec=");
	    builder.append(metaExec);
	    builder.append("]");
	    return builder.toString();
	}

	/** <pre>
	 * Method : Config.getCleanup()
	 * 	to get cleanup
	 * <br> 
	 * @return the cleanup : type of long
	 * </pre>
	 */
	public long getCleanup() {
	    return cleanup;
	}

	/** <pre>
	 * Method : Config.setCleanup()
	 * 	to set cleanup 
	 * <br> 
	 * @param cleanup : type of long
	 * </pre>
	 */
	public void setCleanup(long cleanup) {
	    this.cleanup = cleanup;
	}

	/** <pre>
	 * Method : Config.getAppHome()
	 * 	to get appHome
	 * <br> 
	 * @return the appHome : type of String
	 * </pre>
	 */
	public String getAppHome() {
	    return appHome;
	}


}
